﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bitirme.DAL.Entities;
using Bitirme.DAL.Utils;
using System.Data.SqlClient;
using System.Data;

namespace Bitirme.DAL.Data_Access
{
    public class DependencyCollection
    {
        public static bool InsertDependencyToWorkPackage(WorkPackage workPackage, List<WorkPackage> dependencyList)
        {

            SqlConnection myConnection = null;
            SqlCommand cmd = null;
            SqlParameter parWorkPackageID = null;
            SqlParameter parDependencyID = null;

            foreach (WorkPackage dependency in dependencyList)
            {
                try
                {
                    myConnection = SQLUtils.GetConnection();
                    cmd = new SqlCommand("InsertDependencyToWorkPackage", myConnection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = myConnection; // ??


                    parWorkPackageID = new SqlParameter();
                    parWorkPackageID.ParameterName = "@childID";
                    parWorkPackageID.Value = workPackage.ID;
                    parWorkPackageID.SqlDbType = SqlDbType.Int;

                    parDependencyID = new SqlParameter();
                    parDependencyID.ParameterName = "@parentID";
                    parDependencyID.Value = dependency.ID;
                    parDependencyID.SqlDbType = SqlDbType.Int;

                    cmd.Parameters.Add(parWorkPackageID);
                    cmd.Parameters.Add(parDependencyID);

                    myConnection.Open();
                    cmd.ExecuteScalar();
                
                }
                catch (SqlException e)
                {

                    Console.WriteLine(e);
                    return false;
                }
            }

            if (myConnection != null)
            {

                myConnection.Close();
                myConnection.Dispose();
            }
        
            return true;
        }

        /*** WorkPackages'ın tüm bağımlılıklarının ID listesini döndürür ***/
        public static List<int> GetDependenciesIDByWorkPackagesID(int workpackageID)
        {
            List<int> dependencyIDList = new List<int>();

            SqlConnection myConnection = null;
            SqlDataReader myReader = null;
            SqlParameter parProjectID = null;
            SqlCommand cmd = null;

            try
            {
                myConnection = SQLUtils.GetConnection();
                cmd = new SqlCommand("GetDependenciesByWorkPackagesID", myConnection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = myConnection;

                parProjectID = new SqlParameter();
                parProjectID.ParameterName = "@childID";
                parProjectID.Value = workpackageID;
                parProjectID.SqlDbType = SqlDbType.Int;

                cmd.Parameters.Add(parProjectID);

                myConnection.Open();
                myReader = cmd.ExecuteReader();
                while (myReader.Read())
                {
                    int ID;
                    Int32.TryParse(SQLUtils.NVL(myReader["parentID"], 0).ToString(), out ID);
                    dependencyIDList.Add(ID);
                }
            }

            catch (SqlException e)
            {
                Console.WriteLine(e);
            }

            finally
            {
                if (myReader != null)
                {
                    myReader.Close();
                    myReader.Dispose();
                }

                if (myConnection != null)
                {
                    myConnection.Close();
                    myConnection.Dispose();
                }
            }

            return dependencyIDList;
        }

    }
}